<template>
    <div class="top-box">
        <titleComponent title="计划收入" path="/projectTable" :orgId="orgId" :yearFilter="yearFilter"></titleComponent>
        <div class="center">
            <div class="money-box" v-for="item, i in list">
                <img :src="require(`../../../assets/img/incomeIcon${i + 1}.png`)" />
                <div>
                    <div class="name">{{ item.name }}<span class="unit">({{ item.unit }})</span></div>
                    <div class="value" :style="{ color: item.color }">{{ item.value }}</div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import titleComponent from "./titleComponent";
export default {
    components: {
        titleComponent,
    },
    watch: {
        income(val, oldVal) {
            if (val !== oldVal) {
                this.list = [
                    { name: "累计收入", value: this.getNum(val && val.totalRevenueAmount ? val.totalRevenueAmount : 0), color: '#ffab4b', path: '/projectTable', unit: this.getUnit(val && val.totalRevenueAmount ? val.totalRevenueAmount : 0) },
                    { name: "IT通服收入", value: this.getNum(val && val.itServiceRevenueAmount ? val.itServiceRevenueAmount : 0), color: '#1e8aff', path: '/projectTable', unit: this.getUnit(val && val.itServiceRevenueAmount ? val.itServiceRevenueAmount : 0) },
                    { name: "CT通服收入", value: this.getNum(val && val.ctServiceRevenueAmount ? val.ctServiceRevenueAmount : 0), color: '#ff293f', path: '/projectTable', unit: this.getUnit(val && val.ctServiceRevenueAmount ? val.ctServiceRevenueAmount : 0) },
                    { name: "非通服收入", value: this.getNum(val && val.nonServiceRevenueAmount ? val.nonServiceRevenueAmount : 0), color: '#24e1c0', path: '/projectTable', unit: this.getUnit(val && val.nonServiceRevenueAmount ? val.nonServiceRevenueAmount : 0) }
                ]
            }
        }
    },
    props: {
        income: {
            type: Object,
            default: () => { },
        },
        orgId: {
            type: String,
            default: ''
        },
        yearFilter: {
            type: String,
            default: ''
        },
    },
    data() {
        return {
            list: [
                { name: "累计收入", value: this.getNum(this.income && this.income.totalRevenueAmount ? this.income.totalRevenueAmount : 0), color: '#ffab4b', path: '/projectTable', unit: this.getUnit(this.income && this.income.totalRevenueAmount ? this.income.totalRevenueAmount : 0) },
                { name: "IT通服收入", value: this.getNum(this.income && this.income.itServiceRevenueAmount ? this.income.itServiceRevenueAmount : 0), color: '#1e8aff', path: '/projectTable', unit: this.getUnit(this.income && this.income.itServiceRevenueAmount ? this.income.itServiceRevenueAmount : 0) },
                { name: "CT通服收入", value: this.getNum(this.income && this.income.ctServiceRevenueAmount ? this.income.ctServiceRevenueAmount : 0), color: '#ff293f', path: '/projectTable', unit: this.getUnit(this.income && this.income.ctServiceRevenueAmount ? this.income.ctServiceRevenueAmount : 0) },
                { name: "非通服收入", value: this.getNum(this.income && this.income.nonServiceRevenueAmount ? this.income.nonServiceRevenueAmount : 0), color: '#24e1c0', path: '/projectTable', unit: this.getUnit(this.income && this.income.nonServiceRevenueAmount ? this.income.nonServiceRevenueAmount : 0)}
            ]
        }
    },
    methods: {
        goLink(path) {
            this.$router.push({ path, query: {cityCode: this.orgId == '10008' ? '' : this.orgId} });
        },
        getNum(num) {
            return this.numberFormatter(num, 2).num;
        },
        getUnit(num){
            return this.numberFormatter(num, 2).unit;
        },
        numberFormatter(num, digits) {
            const si = [
                { value: 1E8, symbol: '亿元' },
                { value: 1E4, symbol: '万元' }
            ]
            for (let i = 0; i < si.length; i++) {
                if (num >= si[i].value) {
                    return {num: (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1'), unit: si[i].symbol}
                }
            }
            return {num: num.toString(), unit: '元'}
        },
        truncateToFixed(value, decimals) {
            const integerPart = Math.trunc(value); // 获取整数部分
            const decimalPart = value - integerPart; // 获取小数部分
            const factor = Math.pow(10, decimals);
            const truncatedDecimalPart = decimalPart < 0 ? Math.ceil(decimalPart * factor) / factor : Math.floor(decimalPart * factor) / factor; // 根据正负选择向上或向下截断
            return integerPart + truncatedDecimalPart; // 组合整数部分和小数部分
        }
    }
}
</script>
<style lang="scss" scoped>
.top-box {
    background: #fff;
    width: 100%;
    height: 160px;
    margin-bottom: 16px;
    box-sizing: border-box;
    padding: 16px;
}

.unit {
    color: #a5a7ac;
    font-size: 14px;
    font-weight: 400;
}

.center {
    display: flex;
}

.money-box {
    display: flex;
    margin-top: 30px;
    width: 25%;
    flex-wrap: wrap;

    img {
        margin-right: 12px;
        width: 50px;
        height: 50px;
    }

    .value {
        font-size: 24px;
        color: #ffab4b;
        font-weight: bold;

    }
}
.money-box{
    margin-top: 20px;
}
.top-box{
    height: 130px;
}
.name {
    font-weight: 900;
    font-size: 16px;
}

.pointer {
    cursor: pointer;
}
</style>